import json
import logging
from pathlib import Path
from typing import Any, Dict, List

def load_json_file(file_path: Path) -> List[Dict[str, Any]]:
    """从文件中加载JSON数据"""
    if not file_path.exists():
        logging.warning(f"JSON文件不存在: {file_path}")
        return []
    try:
        with open(file_path, "r", encoding="utf-8") as f:
            return json.load(f)
    except json.JSONDecodeError:
        logging.error(f"无法解析JSON文件: {file_path}")
        return []
    except Exception as e:
        logging.error(f"读取JSON文件时发生未知错误 {file_path}: {e}")
        return []

def save_json_file(file_path: Path, data: List[Dict[str, Any]]):
    """将数据保存为JSON文件"""
    try:
        file_path.parent.mkdir(parents=True, exist_ok=True)
        with open(file_path, "w", encoding="utf-8") as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
        logging.info(f"成功将 {len(data)} 条数据保存到 {file_path}")
    except Exception as e:
        logging.error(f"保存JSON文件时发生错误 {file_path}: {e}")
